package com.kylin.core.user.web.facade.impl;

import com.kylin.core.common.web.facade.AbstractFacade;
import com.kylin.core.user.query.handler.AddressTranslator;
import com.kylin.domain.user.command.CreateUserAddressCommand;
import com.kylin.domain.user.command.FindDefaultAddressCommand;
import com.kylin.domain.user.command.RemoveUserAddressCommand;
import com.kylin.domain.user.provider.UserAddressProvider;
import com.kylin.domain.user.vo.UserAddressVO;
import com.kylin.domain.user.vo.WechatUserInfoVO;
import com.kylin.shared.common.ResponseWrapper;
import com.kylin.shared.common.anno.ArgsValid;
import com.kylin.shared.enums.ResultCodeEnum;
import com.kylin.shared.utils.MappingTools;
import com.kylin.user.api.dto.req.CreateUserAddressReqDTO;
import com.kylin.user.api.dto.req.QueryUserAddressReqDTO;
import com.kylin.user.api.dto.req.UpdateAddressReqDTO;
import com.kylin.user.api.dto.resp.UserAddressRespDTO;
import com.kylin.user.api.web.facade.UserPlatformFacade;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户功能实现
 * @author Damon S.
 * @version v1.0.1
 * @date 2020年05月23日 18:06
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "用户开放接口")
public class UserPlatformFacadeImpl extends AbstractFacade implements UserPlatformFacade {

    private final UserAddressProvider provider;
    private final AddressTranslator translator;

    @ArgsValid
    @Override
    @ApiOperation(value = "收货地址列表", notes = "获取用户收货地址列表")
    public ResponseWrapper<List<UserAddressRespDTO>> listUserAddress(QueryUserAddressReqDTO req) {
        return new ResponseWrapper<>();
    }

    @ArgsValid
    @Override
    @ApiOperation(value = "默认收货地址", notes = "获取用户默认收货地址列表")
    public ResponseWrapper<UserAddressRespDTO> findDefaultAddress() {
        WechatUserInfoVO userInfo = getWxUserInfo();
        if (WechatUserInfoVO.EMPTY.equals(userInfo)) {
            return new ResponseWrapper<>(ResultCodeEnum.USER_NOT_EXIST);
        }
        UserAddressVO address = provider.findAddressDefault(
                new FindDefaultAddressCommand(Long.parseLong(userInfo.getUserId()))
        );
        if (UserAddressVO.EMPTY.equals(address)) {
            return new ResponseWrapper<>(ResultCodeEnum.NOT_FOUND);
        }
        return new ResponseWrapper<>(
                translator.toUserAddressRespDTO(address)
        );
    }

    @Override
    @ApiOperation(value = "删除收货地址", notes = "删除收货地址")
    public ResponseWrapper<Boolean> removeByAddressId(Long addressId) {
        WechatUserInfoVO userInfo = getWxUserInfo();
        if (WechatUserInfoVO.EMPTY.equals(userInfo)) {
            return new ResponseWrapper<>(ResultCodeEnum.USER_NOT_EXIST);
        }
        return new ResponseWrapper<>(provider.removeUserAddressById(
                RemoveUserAddressCommand.builder()
                        .addressId(addressId)
                        .userId(Long.parseLong(userInfo.getUserId()))
                        .removedBy(Long.parseLong(userInfo.getUserId()))
                        .removedAt(MappingTools.toCurrentEpochSecond())
                        .build()
                )
        );
    }

    @ArgsValid
    @Override
    @ApiOperation(value = "更新收货地址", notes = "更新指定收货地址")
    public ResponseWrapper<UserAddressRespDTO> updateUserAddress(UpdateAddressReqDTO req) {
        return new ResponseWrapper<>();
    }

    @ArgsValid
    @Override
    @ApiOperation(value = "新增收货地址", notes = "新增收货地址")
    public ResponseWrapper<Boolean> addUserAddress(CreateUserAddressReqDTO req) {
        WechatUserInfoVO userInfo = getWxUserInfo();
        if (WechatUserInfoVO.EMPTY.equals(userInfo)) {
            return new ResponseWrapper<>(ResultCodeEnum.USER_NOT_EXIST);
        }
        return new ResponseWrapper<>(provider.createUserAddress(
                CreateUserAddressCommand.builder()
                        .userId(Long.parseLong(userInfo.getUserId()))
                        .consignee(req.getConsignee())
                        .postcode(req.getPostCode())
                        .province(req.getProvince())
                        .city(req.getCity())
                        .county(req.getCounty())
                        .phone(req.getPhone())
                        .createdBy(Long.parseLong(userInfo.getUserId()))
                        .address(req.getAddress())
                        .build())
        );
    }
}
